#!/usr/bin/env python
# -*- coding:utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat

import matplotlib
import scipy.optimize as opt
from sklearn.metrics import classification_report

data = loadmat('./dataset/ex4data1.mat')
X = data['X']
y = data['y']
print(X.shape, y.shape)

#可视化数据部分
def display(x):
    (m, n) = x.shape   #100*400
    width = np.round(np.sqrt(n)).astype(int)
    height = (n / width).astype(int)

    gap = 1  #展示图像间的距离
    display_array = -np.ones((gap + 10 * (width + gap), gap + 10 * (height + gap)))
    # 将样本填入到display矩阵中
    curr_ex = 0
    for j in range(10):
        for i in range(10):
            if curr_ex > m:
                break
            # Get the max value of the patch
            max_val = np.max(np.abs(x[curr_ex]))
            display_array[gap + j * (height + gap) + np.arange(height),
                          gap + i * (width + gap) + np.arange(width)[:, np.newaxis]] = \
                x[curr_ex].reshape((height, width)) / max_val
            curr_ex += 1
        if curr_ex > m:
            break
    plt.figure()
    plt.imshow(display_array, cmap='gray', extent=[-1, 1, -1, 1])
    plt.show()

# 随机抽取100个训练样本 进行可视化
m = y.size
rand_indices = np.random.permutation(range(m))  # 获取0-4999 5000个无序随机索引
selected = X[rand_indices[0:100], :]  # 获取前100个随机索引对应的整条数据的输入特征
print(selected.shape)
display(selected)